package com.ls.filter;

import com.ls.pojo.User;
import com.ls.util.constant.Constant;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 李朔
 * @version 1.0
 */
public class SysFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest) servletRequest;
        HttpServletResponse resp=(HttpServletResponse)servletResponse;

        //从Session中获取用户
        User user=(User) req.getSession().getAttribute(Constant.USER_SESSION);

        if(user==null){
            //说明用户已经被移除或者注销了，或者未登录
            resp.sendRedirect(req.getContextPath()+"/jsp/error.jsp");
        }else{
            filterChain.doFilter(req,resp);
        }


    }

    public void destroy() {

    }
}
